﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	public class BorrowDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Borrow.GetAll);
		}

		public static DataTable GetOne(int borrowId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ID", SqlDbType.Int, borrowId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Borrow.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}
        public static DataTable GetAll(int range)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@Range", SqlDbType.Int, range)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Borrow.GetAllWithDateRange, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

		public static Borrow LoadOne(int borrowId)
		{
			Borrow o = new Borrow();
			DataTable dt = GetOne(borrowId);
			if(dt.Rows.Count > 0)
			{
				o.ID = int.Parse(dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.ID].ToString());
				o.Date = DateTime.Parse(dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.Date].ToString());
				o.ToolID = dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.ToolID].ToString();
				o.StaffID = dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.StaffID].ToString();
				o.MachineID = dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.MachineID].ToString();
			    //o.Quantity = int.Parse(dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.Quantity].ToString());
                o.Quantity = !Utilities.IsNullOrEmpty(dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.Quantity].ToString()) ? int.Parse(dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.Quantity].ToString()) : 0;
				o.Note = dt.Rows[0][ToolConstants.Entities.Borrow.FieldName.Note].ToString();

			}

			return o;
		}


		public static int Insert(Borrow o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@StaffID", SqlDbType.NVarChar, o.StaffID),
                        CreateParameter("@MachineID", SqlDbType.NVarChar, o.MachineID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Borrow.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

		public static int Update(Borrow o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@StaffID", SqlDbType.NVarChar, o.StaffID),
                        CreateParameter("@MachineID", SqlDbType.NVarChar, o.MachineID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Borrow.Update, parameters);
			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}
		}

		public static int Delete(Borrow o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Borrow.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Borrow");

			return table;
		}

		#endregion
	}
}
